window.onload = function () {

    Ajax.get(`http://127.0.0.1:8000/getSKU?id=${getCookie('id')}`, (data) => {
        let data1 = JSON.parse(data)
        let str = `
<div class="header">
<div class="top">
<a href="http://127.0.0.1:8000/html/shopCart.html" target="_blank"><span>购物车</span></a>
<a href="http://127.0.0.1:8000/html/login.html"><span>请登录</span></a>
<a href="http://127.0.0.1:8000/html/register.html"><span>免费注册</span></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span id="span" style="color: #333;"></span>
<a href="http://127.0.0.1:8000/html/login.html" id="logout"><span id="span1">登出</span></a>
<a href="http://127.0.0.1:8000/html/index.html" id="logout"><span id="span1">返回首页</span></a>
</div>
</div>
<div class="floatDiv">成功加入到购物车!!!</div>
<div class="img left"><img src="${data1.img}" alt=""></div>
<div class="content left">
<h3 class="title">${data1.title}</h3>
<div class="item1"><span class="span1">价格</span><span class="span2">￥${data1.price}</span></div>
<div class="item2"><span class="span1">运费</span><span class="span2">包邮（新疆、西藏、青海、内蒙古、海外除外）</span></div>

<div class="item4">
    <div>颜色分类</div>
    <div>黄色</div>
    <div>蓝色</div>
    <div>白色</div>
</div>
<div class="item4">
    <div>尺码</div>
    <div>L</div>
    <div>M</div>
    <div>XL</div>
</div>
<div class="item5">
    <span>数量</span>
    <span>
        <input type="text" value="1" maxlength="8">
        <span class="add_minus">
            <span class="add">+</span><span class="minus">-</span>
        </span>
    </span>
    <span class="piece">件</span>
</div>
<div class="item6">
    <div class="addCart">加入购物车</div>
</div>
</div>`
        document.body.innerHTML = str;
        let add = document.getElementsByClassName('add')[0];
        let minus = document.getElementsByClassName('minus')[0];
        let input = document.getElementsByTagName('input')[0];
        add.onclick = function () {
            input.value++;
        };
        minus.onclick = function () {
            input.value--;
            if (input.value < 1) {
                input.value = 1;
            }
        };
        function sendData(data1) {
            data1.number = parseInt(input.value)
            console.log(data1.number)
            let CartData = JSON.stringify(data1);
            Ajax.post('http://127.0.0.1:8000/showCart', CartData, (data) => {

            })
        }
        let span = document.getElementById("span")
        let span1 = document.getElementById("span1")
        let dataMsg = JSON.parse(localStorage.getItem("dataMsg"))
        let sessionid = getCookie("sessionid")
        if (sessionid != null) {
            span.innerHTML = "你好，" + dataMsg.msg.userInfo.name
            span1.innerHTML = "登录"
        } else {
            span1.innerHTML = ""
        }
        let addCart = document.getElementsByClassName("addCart")[0];
        let floatDiv = document.getElementsByClassName("floatDiv")[0];
        addCart.onclick = function () {
            if (sessionid != null) {
                sendData(data1);
                floatDiv.style.display = "block";
                setTimeout(() => {
                    floatDiv.style.display = "none";
                }, 500)
            } else {
                window.location.href = "http://127.0.0.1:8000/html/login.html"
            }

        }
        let logout = document.getElementById('logout')
        let username = getCookie("username")
        logout.onclick = function () {
            if (username != null) {
                Ajax.post('http://127.0.0.1:8000/logout', JSON.stringify(username), (data) => {
                    console.log(data)
                })
            }
        }
    })
    function getCookie(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }

}